<html>
<head>
	<title>BlackJack</title>
	<style type ="text/css">
	</style>
	<script type = "text/javascript">
	
	var playerIndex = new Array(); //playerindex[0] = {playerHand,playerHandSuit,playerHandValueHard,playerHandValueSoft,stillIn,won}
	var playerHand = new Array();
	var playerHandSuit = new Array();
	var dealerHand = new Array();
	var dealerHandSuit = new Array();
	var playerHandValueSoft = 0;
	var playerHandValueHard = 0;
	var playerHandValue = 0;
	var dealerHandValueSoft = 0;	
	var dealerHandValue = 0;
	var dealerHandValueHard = 0;
	var dealerCardNumber=0;
	var playerCardNumber=0;
	var playerNumber=0;
	var numberOfPlayers = 1;
	var stillIn = true;
	var dealerStillIn = true;
	var winner = 0;
	var gameGoing = true;
	


	function random(x,y){
		return Math.floor(Math.random()*(y-x)+x);
						}

	function gameStart(){

				playerNumber=0;
				dealerHit();
				playerStart();

				document.getElementById('Controls').innerHTML = "<input type= 'button' value = 'hit' onClick='playerhit()'/><input type= 'button' value='stick' onClick='stick()'/><input type='button' value='restart' onClick='reload()'/>";
				//document.getElementById("Controls").innerHTML ="<input type= 'button' value='stick' onClick='stick()'/>";
				return;							 
						}

	function playerStart(){
		playerhit();
		playerhit();
		return;
		
	}
	function reload(){
		window.location.reload();

	}

	function doubleDown(){
		if(gameGoing){
			playerhit(1);
			stick();
		}
		else{
			alert("The game is over!")
		}
		return;
	}

	function dealer(){
		
		gameGoing = victoryCheck(2);
		while(dealerHandValueHard<16 && (dealerHandValueSoft<16 || dealerHandValueSoft>21) && dealerStillIn && gameGoing){
			dealerHit();
		}
		if(gameGoing)
		{
			if(dealerStillIn)
			{
				if((dealerHandValueSoft != dealerHandValueHard) && dealerHandValueSoft<21){
					alert("Dealer sticks at:"+dealerHandValueHard + "/" + dealerHandValueSoft);
				}else{
					alert("Dealer sticks at:" + dealerHandValueHard);
				}
			}
		
		gameGoing = victoryCheck(0);
	}
		return;
		}
	
	

	function victoryCheck(j){
			var x;
			var c=0;
			var i;
			var whoWon = new Array();
			var whoTild = new Array();
			var whoLost = new Array();


			if(j==0 || j==2){
				for(i in playerIndex){
				x = playerIndex[i];
				if (x[5])
				{
					c= 1;

				}
			}

			if(c===0){
					alert("Everyone is out! Dealer wins!");
					return false;
			}
			if(j==2)
			{
				return true;
			}

			var k = 0 ;
			var kc = 0;
			var kcc = 0;
			for(i in playerIndex)
			{ 
				x = playerIndex[i];
				if (dealerHandValueSoft <21)
				{

					if(x[4] <21 && x[4] > dealerHandValueHard &&  x[4]>dealerHandValueSoft)
					{
					whoWon[k]=i;
					k=k+1;
					}
					else if(x[4]<21 && x[4] >= dealerHandValueHard)
					{
					whoTild[kc] = i; 
					kc=kc+1;
					}
				
					else
					{
					whoLost[kcc]=i;
					kcc = kcc+1;
					}
				}
			
			else{
				if((x[4]<21 && x[4] > dealerHandValueHard)|| (x[3]> dealerHandValueSoft))
				{
					whoWon[k]=i;
					k=k+1;
				}
				else if((x[4]<21&&x[4]>=dealerHandValueHard)||(x[3]> dealerHandValueSoft))
				{
					whoTild[kc]=i;
					kc = kc+1;
				}
				else{
					whoLost[kcc]=i;
					kcc=kcc+1;
				}
				}
			
			}
			for (i in whoWon){
				alert("Player"+ (Number(whoWon[i])+1)+" won");
			}
			for (i in whoTild){
				alert("player"+ (Number(whoTild[i])+1)+" tild");
			}
			for (i in whoLost){
				alert("Player "+ (Number(whoLost[i])+1)+" lost");
			}

}

	
	else if(j==1){
		var piz = 0;
		for(i in playerIndex){
			x=playerIndex[i];
			if(x[5]){
				c=1;
				whoWon[piz] = i;
				piz = piz+1;
			}
			if(i===0){
				alert("Everyone is out, noboy wins");
				}
				else{
					for (i in whoWon){
						alert("Player" + (Number(whoWon[i])+1)+"won");
					}
				}
			}
			return false;
		}
	
}	



	
	function stick(){

			if(gameGoing){
			playerIndex[playerNumber]= [playerHand,playerHandSuit,playerCardNumber,playerHandValueHard,playerHandValueSoft,stillIn,winner];
			playerNumber = playerNumber+1;
			if(playerNumber >= numberOfPlayers)
			{
				dealer();

			}
			else {
				playerHandValueHard = 0;
				playerHandValueSoft = 0;
				playerCardNumber=0;
				playerHand = new Array();
				playerHandSuit = new Array()
				playerStart();

				}
			}
			else{alert("The game is over!")
		}
	return;
}
	

	function dealerHit(){
						
	
						var suit = randomSuit();
						var card = randomCard(); 

						dealerHand[dealerCardNumber]=card[0];
						dealerHandSuit[dealerCardNumber]=suit;
						dealerHandValueHard = dealerHandValue + card[1];
						dealerHandValueSoft = dealerHandValueSoft + card[2];
						dealerCardNumber = dealerCardNumber + 1;
						dealerStillIn= bustCheck(dealerHandValueHard,0)
						updateDealerHand();
						if(!dealerStillIn)
						{
								gameGoing = victoryCheck(2);
						}
					
					
						return;
	}


	function playerhit(doubleDown)
			{			
				if(gameGoing){

						var suit = randomSuit();
						var card = randomCard(); 
						playerHand[playerCardNumber]=card[0];
						playerHandSuit[playerCardNumber]=suit;
						playerHandValueHard = playerHandValueHard + card[1];
						playerHandValueSoft = playerHandValueSoft + card[2];
						playerCardNumber = playerCardNumber+1;
						stillIn = bustCheck(playerHandValueHard,1);
						updateHand();
						if(!stillIn && doubleDown!=1)
							{
								stick();
							}
						}else{ 
							alert("The Game is over!");
						}
						return;

			}

	function updateHand(){
		var b;
		var i;
		var m=0;


		for(i in playerHand){
			if(m<1)
			{b= playerHand[i]+ " of " + playerHandSuit[i];
				m=m+1;
				}
		else{

			b = b + " " + playerHand[i]+" of " + playerHandSuit[i];

		}}
		if(playerHandValueHard != playerHandValueSoft){


		document.getElementById("activePlayerHand").innerHTML= "Player Hand: " + b +  " Player Hand Value:"+playerHandValueHard+ "/" + playerHandValueSoft;
													}
		else{

		document.getElementById("activePlayerHand").innerHTML= "Player Hand: " + b +  " Player Hand Value:"+playerHandValueHard;
													}

}
			

	function updateDealerHand(){
		var b;
		var i;
		var m=0;
		for(i in dealerHand){
			if(m<1){
				b=dealerHand[i]+ " of " + dealerHandSuit[i];
				m=m+1;
			}
			else{
			b = b + " " + dealerHand[i]+" of " + dealerHandSuit[i];
}
		}
		if(dealerHandValueHard != dealerHandValueSoft){


		document.getElementById("dealerHand").innerHTML= "Dealer Hand " + b + " Dealer Hand Value:"+dealerHandValueHard+ "/" + dealerHandValueSoft;
													}
		else{

		document.getElementById("dealerHand").innerHTML= "Dealer Hand " + b +  " Dealer Hand Value::"+dealerHandValueHard;
													}

	}
	function bustCheck(x,y)
	{
		if (x >21)
		{
			if(y == 1){
			alert(" Over 21 You lose");
			} 
				else{
				alert("Dealer is out")
			}
			return false;
		}
		else{ 
			return true;
		}
	}

			
	function randomSuit(){
		// Zufällige Kartenfarbe wird generiert
		rand = random(0,3);
			switch(rand){
			case 0:
				suit = "Hearts"  //Herz
				break;
			case 1:
				suit = "Spades" //Pik
				break;
			case 2:
				suit = "Spades" // Karo
				break;
			case 3:
				suit = "Clubs" // Kreuz
				break;
			default:
			alert("Error Farbgebung");

		}
		return suit;
	}

	function randomCard(){
		// Zufällige Kartenfarbe wird generiert
		var cardArray = new Array();
		var randCard = random(1,13);
			switch(randCard){
			case 1:
				cardArray[0] = "A"  // shown value
				cardArray[1] = 1   //hard value
				cardArray[2] = 11	//soft value
				break;
			case 11:
				cardArray[0] = "J" 
				cardArray[1] = 10;
				cardArray[2] = 10;

				break;
			case 12:
				cardArray[0] = "Q" 
				cardArray[1] = 10;
				cardArray[2] = 10;
				break;
			case 13:
				cardArray[0] = "K" 
				cardArray[1] = 10;
				cardArray[2] = 10;
				break;
			default:
				cardArray[0]=randCard;
				cardArray[1]=randCard;
				cardArray[2]=randCard;

		}
		return cardArray;
	}



	
	</script>
</head>
<body>
<div id = "Main">
	<p>Welcome to BlackJack X</p>
	
	<div id = "dealerHand">Dealer currently has no cards </div>
	<div id = "activePlayerHand">Currently no active Player</div>
	<div id = "Controls">
	<input type='button' value = 'start' onClick='gameStart()'/>

	</div>	
</div>
</body>
</html>